package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.model.Admin;
import com.cskaoyan.mall.model.Result;
import com.cskaoyan.mall.model.bo.admin.AdminAddBO;
import com.cskaoyan.mall.model.bo.admin.AdminLoginBO;
import com.cskaoyan.mall.model.bo.admin.ChangePwdBO;
import com.cskaoyan.mall.model.bo.admin.SearchAdminBO;
import com.cskaoyan.mall.model.vo.admin.AdminLoginVO;
import com.cskaoyan.mall.service.admin.AdminService;
import com.cskaoyan.mall.service.admin.AdminServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.cskaoyan.mall.utils.StringUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/admin/admin/*")
/**
 * 这是管理员模块的内容，登录admin账号之后生成进入该servlet
 */
public class AdminServlet extends HttpServlet {
    private Gson gson = new Gson();
    private AdminService adminService = new AdminServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/admin/","");
        if ("login".equals(action)) {
            login(request,response);
        }else if ("getSearchAdmins".equals(action)) {
            getSearchAdmins(request, response);
        }else if ("addAdminss".equals(action)) {
            addAdminss(request,response);
        }else if ("updateAdminss".equals(action)) {
            updateAdminss(request,response);
        }else if ("changePwd".equals(action)) {
            changePwd(request,response);
        }
    }

    /**
     * 修改个人资料
     * @param request
     * @param response
     */
    private void changePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        ChangePwdBO changePwdBO = gson.fromJson(requestBody,ChangePwdBO.class);
        if (!changePwdBO.getNewPwd().equals(changePwdBO.getConfirmPwd())) {
            response.getWriter().println(gson.toJson(Result.error("请再次确认密码！")));
            return;
        }
        int code = adminService.changePwd(changePwdBO);
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("修改失败！")));
    }

    /**
     * 在进行update之前，先进行获取操作，获取id=?的用户信息
     * @param request
     * @param response
     */
    private void getAdminsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        //对请求报文进行处理
        //这里由于修改和添加的所需的字段相同，所以直接采用这个
        Admin admin = adminService.getAdminInfo(id);
        if (admin != null) {
            response.getWriter().println(gson.toJson(Result.ok(admin)));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("获取用户数据失败")));
    }

    /**
     * 修改指定的那个啥管理员的信息
     * @param request
     * @param response
     */
    private void updateAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        //对请求报文进行处理
        //这里由于修改和添加的所需的字段相同，所以直接采用这个
        Admin admin = gson.fromJson(requestBody,Admin.class);
        if (StringUtils.isEmpty(admin.getEmail()) || StringUtils.isEmpty(admin.getNickname()) || StringUtils.isEmpty(admin.getPwd())) {
            response.getWriter().println(gson.toJson(Result.error("输入的参数不能为空")));
            return;
        }
        int code = adminService.update(admin);
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("该账号不允许重复使用")));
    }

    /**
     * 添加管理员账号
     * @param request
     * @param response
     */
    private void addAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        //对请求报文进行处理
        AdminAddBO adminAddBO = gson.fromJson(requestBody,AdminAddBO.class);
        if (StringUtils.isEmpty(adminAddBO.getEmail()) || StringUtils.isEmpty(adminAddBO.getNickname()) || StringUtils.isEmpty(adminAddBO.getPwd())) {
            response.getWriter().println(gson.toJson(Result.error("输入的参数不能为空")));
            return;
        }
        int code = adminService.add(adminAddBO);
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("该账号不允许重复使用")));
    }

    /**
     * 多条件查询admin信息
     * 1.获取请求参数 --json字符串
     * 2.处理 -- MVC三层架构 sql
     * 3.响应
     * @param request
     * @param response
     */
    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        SearchAdminBO searchAdminBO = gson.fromJson(requestBody,SearchAdminBO.class);
        //如果什么都没有输入，那么直接返回message并显示
        if (StringUtils.isEmpty(searchAdminBO.getEmail()) && StringUtils.isEmpty(searchAdminBO.getNickname())) {
            response.getWriter().println(gson.toJson(Result.error("输入的参数不能为空")));
            return;
        }
        List<Admin> admins = adminService.searchAdmins(searchAdminBO);
        response.getWriter().println(gson.toJson(Result.ok(admins)));
    }

    /**
     * 管理员登录的业务逻辑
     * bo vo bean
     * bo business object
     * vo view object
     * bean:与数据库对应
     * 一个视图可能有多个model组合而成
     * vo 里面的数据可能是多个bean杂糅在一起的结果 向客户端输出
     * bo 向客户端输入
     * 一个接口中一般对应三个对象
     * bo用来处理客户端输入
     * bean一般与数据库的表字段一一对应
     * vo 根据实际的情况，页面需要哪些，构建哪些，jd 我的 order info (只返回部分的数据)
     * @param request
     * @param response
     * @throws IOException
     */
    private void login(HttpServletRequest request,HttpServletResponse response) throws IOException {

        String requestBody = HttpUtils.getRequestBody(request);
        //BO用来处理客户端的输入，这里对请求报文进行处理
        AdminLoginBO loginBO = gson.fromJson(requestBody,AdminLoginBO.class);
        if (StringUtils.isEmpty(loginBO.getEmail())|| StringUtils.isEmpty(loginBO.getPwd())) {
            //返回响应结果
            response.getWriter().println(gson.toJson(Result.error("参数不能为空")));
            return;
        }
        int code = adminService.login(loginBO);
        if (code == 200) {
            request.getSession().setAttribute("username",loginBO.getEmail());
            response.getWriter().println(gson.toJson(Result.ok(new AdminLoginVO(loginBO.getEmail(),loginBO.getEmail()))));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("请确认用户名和密码")));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/admin/","");
        if ("allAdmins".equals(action)) {
            allAdmins(request,response);
        }else if ("getAdminsInfo".equals(action)) {
            getAdminsInfo(request,response);
        }else if ("deleteAdmins".equals(action)) {
            deleteAdmins(request,response);
        }
    }

    /**
     * 删除管理员账号
     * @param request
     * @param response
     */
    private void deleteAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        //对请求报文进行处理
        //这里由于修改和添加的所需的字段相同，所以直接采用这个
        int code = adminService.deleteAdmins(id);
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("删除失败")));
    }

    private void allAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Admin> adminList = adminService.allAdmins();
        response.getWriter().println(gson.toJson(Result.ok(adminList)));
    }
}
